<?php
defined('BASEPATH') OR exit('No direct script access allowed');
	class 	Catalog extends CI_Controller {
		public $page_name="catalog";
		public function __construct(){
			parent::__construct();
			if(!$this->session->userdata('is_login')){	//获取session数据
	        	redirect('/admin/auth/login');
	    	}
        }	
		public function index(){
			$this->load->model('Catalog_model');
			$catalogs=$this->Catalog_model->get_catalog();
			$data = array(
				'catalogs'=>$catalogs,
			);
			$this->load->view('admin/Catalog_index',$data);
		}
		public function add(){
            $this->load->library('form_validation');
            $this->form_validation->set_rules('name', '类别', 'required');
             if ($this->form_validation->run() == FALSE){ 
	            $this->load->model("Catalog_model");
	         	$catalogs = new stdClass();
	         	$catalogs->id="";
	         	$catalogs->name="";
	         	$data = array(
	         		'catalogs'=>$catalogs
	         		);
	         	$this->load->view('admin/Catalog_form',$data);
            } else{
		        $data = $this->input->post();
	        	$this->load->model("Catalog_model");
	            $this->Catalog_model->add_one($data);
	            redirect('admin/catalog/index');
			}
		}
		public function edit($id){
            $this->load->library('form_validation');
            $this->form_validation->set_rules('name', '类别', 'required');
             if ($this->form_validation->run() == FALSE){ 
	            $this->load->model("Catalog_model");
	         	$catalogs = $this->Catalog_model->get_one($id);
	         	$data = array(
	         		'catalogs'=>$catalogs
	         		);
	         	$this->load->view('admin/Catalog_form',$data);
            } else{
		        $data = $this->input->post();
	        	$this->load->model("Catalog_model");
	            $this->Catalog_model->edit_one($id,$data);
	            redirect('admin/catalog/index');
			}
		}		
		public function del($id){
			$this->load->model('Catalog_model');
			$this->Catalog_model->del_id($id);
			redirect('admin/catalog/index');
		}
	}